﻿using System;
using System.Windows.Forms;
using System.Threading;
using GestorSQL.CaseFlowIntegration.Interface;
using GestorSQL.Utility;
using System.Collections.Generic;
using System.Collections;
using GestorSQL.GUI.Properties;
using GestorSQL.BLL.ImplementationsBridge;


namespace GestorSQL.GUI
{
    public partial class NewCaseWindow : Form
    {
        public string CaseNumber { get; set; }
        public string OwnerUserName { get; set; }

        private ICaseFlowTool caseFlowTool = null;

        public NewCaseWindow()
        {
            InitializeComponent();
            caseFlowTool = ImplementationBridge.GetCaseFlowImplementation();
        }

        #region Event Handlers
        private void NewCaseWindow_Load(object sender, EventArgs e)
        {
            txtUserName.Text = Thread.CurrentPrincipal.Identity.Name;
            List<string> caseNumebers = caseFlowTool.GetCaseNumberList(txtUserName.Text);

            if ("AllowUserWriteCaseNumber".GetFromAppCfg<bool>())
            {
                txtCaseNumber.Visible = true;
                txtCaseNumber.Focus();
            }
            else
            {
                cmbCaseNumber.Visible = true;
                cmbCaseNumber.Focus();
                if (caseNumebers.Count > 0)
                {
                    cmbCaseNumber.DisplayMember = "String";
                    cmbCaseNumber.ValueMember = "String";
                    cmbCaseNumber.DataSource = caseNumebers;
                }
            }
        }

        private void btAceptar_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
                return;
            CaseNumber = txtCaseNumber.Text;
            OwnerUserName = txtOwnerUserName.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void CaseNumber_Selected(object sender, EventArgs e)
        {
            if (ValidateCaseNumber(false))
            {
                txtOwnerUserName.Text = caseFlowTool.GetCaseOwnerUserName(txtCaseNumber.Text.Trim());
                if (string.IsNullOrEmpty(txtOwnerUserName.Text.Trim()))
                    txtOwnerUserName.ReadOnly = false;
            }
        }

        private void txtOwnerUserName_Leave(object sender, EventArgs e)
        {
            ValidateOwner(false);
        }
        #endregion
        #region Private Methods
        private bool ValidateCaseNumber(bool validateEmpty)
        {
            string caseNumber = txtCaseNumber.Text.Trim();
            if (!validateEmpty && string.IsNullOrEmpty(caseNumber))
                return true;

            if (string.IsNullOrEmpty(caseNumber))
            {
                if (cmbCaseNumber.Visible)
                {
                    MessageBox.Show(Resources.Case_EmptyCaseNumber_cmb);
                    cmbCaseNumber.Focus();
                }
                else
                {
                    MessageBox.Show(Resources.Case_EmptyCaseNumber_txt);
                    txtCaseNumber.Focus();
                }
                return false;
            }
            if (!caseFlowTool.IsValidCaseNumber(txtUserName.Text, caseNumber))
            {
                MessageBox.Show(Resources.Case_InvalidCaseNumber);
                if (cmbCaseNumber.Visible)
                    cmbCaseNumber.Focus();
                else
                    txtCaseNumber.Focus();
                return false;
            }

            return true;
        }

        private bool ValidateOwner(bool validateEmpty)
        {
            string owner = txtOwnerUserName.Text.Trim();
            if (!validateEmpty && string.IsNullOrEmpty(owner))
                return true;

            if (string.IsNullOrEmpty(owner))
            {
                MessageBox.Show(Resources.Case_EmptyOwner);
                txtOwnerUserName.Focus();
                return false;
            }
            if (!caseFlowTool.IsValidUserName(owner))
            {
                MessageBox.Show(Resources.Case_InvalidOwner);
                txtOwnerUserName.Focus();
                return false;
            }

            return true;
        }

        private bool ValidateForm()
        {
            bool isvalid = ValidateCaseNumber(true);
            if (isvalid)
                isvalid = ValidateOwner(true);
            return isvalid;
        }

        #endregion

    }
}
